using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using PointBridge.SPDX;
using Microsoft.SharePoint.Navigation;
using MyNamespace;

namespace SPCmdTest
{
    class SPCmdTestProgram
    {
        static private void TestCT(SPDXContentType item)
        {
            //url
            item.HyperlinkorPicture.ChangeLink("http://www.lendingarsenal.com", "LA");
            
            //choice
            item.Choice2EmptyAllowed.ChangeSelection(SPDXContentType.Choice2_EnterChoice1);

            //Multi-choice
            MultiChoice choices = item.ChoiceEmptyAllowed;
            choices.SetEmpty();
            choices.AddCustomStringSelection("ABC");//SPDXContentType.Choice_EnterChoice1);
            choices.AddSelection(SPDXContentType.Choice_EnterChoice1);
            choices.AddSelection(SPDXContentType.Choice_EnterChoice2);
            choices.AddSelection(SPDXContentType.Choice_EnterChoice3);
            choices.RemoveSelection(SPDXContentType.Choice_EnterChoice3);

            //lookup
            item.AssociateLookup.ChangeSelection(3);

            //Lookup
            Lookup lookup = item.Lookup2EmptyAllowed;
            lookup.ChangeSelection(2);
            SPDXContentType itemx = new SPDXContentType(lookup.GetReferencedItem());
            lookup.SetEmpty();

            //multilookup
            MultiLookup multiLookup = item.LookupEmptyAllowed;
            multiLookup.SetEmpty();
            multiLookup.AddSelection(1);
            multiLookup[0].ChangeSelection(2);
            //multiLookup.RemoveSelection(2);

            //user or group
            User user = item.PersonorGroup2EmptyAllowed;
            user.ChangeSelection(1);
            Person person = new Person(user.GetReferencedItem());
            if (user.GetReferncedUserType() == User.Type.Person)
            {
                SPUser spUser = user.GetReferencedSPUser();
            }
            if (user.GetReferncedUserType() == User.Type.SharePointGroup)
            {
                SPGroup spGroup = user.GetReferncedSPGroup();
            }

            //multi-user select
            MultiUser multiUser = item.PersonorGroupEmptyAllowed;
            multiUser.SetEmpty();
            multiUser.AddSelection(1);
            multiUser.AddSelection(3);
            multiUser.RemoveSelection(3);
            
            item.UpdateWithValidate();
        }
        static private void TestList(SPDXTestListItem item)
        {
            //url
            item.HyperlinkorPicture.ChangeLink("http://www.lendingarsenal.com", "LA");
            
            //choice
            item.Choice2EmptyAllowed.ChangeSelection(SPDXTestListItem.Choice2_EnterChoice1);
            
            //lookup
            item.AssociateLookup.ChangeSelection(2);

            //Multi-choice
            MultiChoice choices = item.ChoiceEmptyAllowed;
            choices.SetEmpty();
            choices.AddCustomStringSelection("ABC");//SPDXContentType.Choice_EnterChoice1);
            choices.AddSelection(SPDXTestListItem.Choice_EnterChoice1);
            choices.AddSelection(SPDXTestListItem.Choice_EnterChoice2);
            choices.AddSelection(SPDXTestListItem.Choice_EnterChoice3);
            choices.RemoveSelection(SPDXTestListItem.Choice_EnterChoice3);

            //Lookup
            Lookup lookup = item.Lookup2EmptyAllowed;
            lookup.ChangeSelection(2);
            item.UpdateWithValidate();
            SPDXContentType itemx = new SPDXContentType(lookup.GetReferencedItem());
            lookup.SetEmpty();

            //multilookup
            MultiLookup multiLookup = item.LookupEmptyAllowed;
            multiLookup.SetEmpty();
            multiLookup.AddSelection(1);
            multiLookup[0].ChangeSelection(2);
            //multiLookup.RemoveSelection(2);

            //user or group
            User user = item.PersonorGroup2EmptyAllowed;
            user.ChangeSelection(1);
            Person person = new Person(user.GetReferencedItem());
            if (user.GetReferncedUserType() == User.Type.Person)
            {
                SPUser spUser = user.GetReferencedSPUser();
            }
            if (user.GetReferncedUserType() == User.Type.SharePointGroup)
            {
                SPGroup spGroup = user.GetReferncedSPGroup();
            }

            //multi-user select
            MultiUser multiUser = item.PersonorGroupEmptyAllowed;
            multiUser.SetEmpty();
            multiUser.AddSelection(1);
            multiUser.AddSelection(3);
            multiUser.RemoveSelection(3);

            item.UpdateWithValidate();
        }
        private static void ActivationManager(SPWeb web)
        {
            SPDXFeatureReceverHelper frHelper = new SPDXFeatureReceverHelper();
            Console.WriteLine("To Activate press 1, to Deactivate press 2, to Exit press 3");
            while (true)
            {
                ConsoleKeyInfo key = Console.ReadKey();
                if (key.KeyChar == '1')
                {
                    Console.WriteLine("\nActivating...");
                    frHelper.Activate(web);
                    Console.WriteLine("Done!\n");
                }
                if (key.KeyChar == '2')
                {
                    Console.WriteLine("\nDeactivating...");
                    frHelper.Deactivate(web);
                    Console.WriteLine("Done!\n");
                }
                if (key.KeyChar == '3') break;
            }
        }

        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://vpc1:4800"))
            {
                using (SPWeb web = site.OpenWeb("SPDX"))
                {
                    SPList list = web.Lists["SPDX Test"];
                    SPListItem item = list.Items[0];
                    
                    /*
                    item["Title"] = "ABC";
                    item.Update();
                    item["Title"] = "BBC";
                    item["Title"] = null;
                    item["Title"] = "NBC";
                    item["Title"] = null;
                    item.Update();                    
                    */

                    TestCT(new SPDXContentType(item));
                    TestList(new SPDXTestListItem(item));

                    //ActivationManager();

                }
            }
        }
    }
}
